const util = require("../../utils/util.js");
Page({
  data: {
    users: [],
    userItems: [{
      icon: '../../images/iconfont/iconfont-dingdan.png',
      text: '我的团队',
      isunread: false,
      unreadNum: 3
    }, {
      icon: '../../images/iconfont/iconfont-card.png',
      text: '打赏鼓励',
      isunread: false,
      unreadNum: 2
    }, {
      icon: '../../images/iconfont/iconfont-icontuan.png',
      text: '联系我们',
      isunread: false,
      unreadNum: 1
    }]
  },
  onLoad: function (options) {
    util.showLoading();
    var that = this;
    var interval = setInterval(function () {
      if (that.data.users.id) {
        clearInterval(interval);
        that.onNickname();
      } else {
        wx.getStorage({
          key: 'USERS',
          success: function(res) {
            that.setData({
              users: res.data
            });
          }
        })
      }
    }, 1 * 1500);
    util.hideToast();
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  onNickname: function () {//更新昵称
    var that = this;
    var params = {
      "id": that.data.users.id,
      "nickName": that.data.users.nickName
    }
    wx.getUserInfo({
      withCredentials: true,
      lang: "zh_CN",
      success: function (res) {
        var params = {
          "id": that.data.users.id,
          "nickName": res.userInfo.nickName,
          "avatarUrl": res.userInfo.avatarUrl,
          "gender": res.userInfo.gender
        }
        if (!that.data.users.nickname && that.data.users.nickname != params.nickName) {
          util.wxPost({
            url: "/rest/lufei/muser",
            params: params,
            success: function (res) {
              if (res.data.status == "000") {
                that.setData({
                  users: res.data.data
                });
                wx.setStorage({
                  key: 'USERS',
                  data: res.data.data
                });
              }
            }
          });
        }
      }
    });
  }
});